package open.tech.dawn.rest.module.system.controller;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import open.tech.dawn.core.error.ServiceException;
import open.tech.dawn.rest.common.Const;
import open.tech.dawn.rest.common.annot.Menu;
import open.tech.dawn.rest.common.page.PageFactory;
import open.tech.dawn.rest.common.page.PageInfo;
import open.tech.dawn.rest.module.system.entity.RoleAuthority;
import open.tech.dawn.rest.module.system.entity.ZTreeNode;
import open.tech.dawn.rest.module.system.model.SysRole;
import open.tech.dawn.rest.module.system.model.SysUser;
import open.tech.dawn.rest.module.system.query.RoleQuery;
import open.tech.dawn.rest.module.system.service.ISysRoleService;
import open.tech.dawn.rest.module.system.service.ISysUserService;
import open.tech.dawn.web.core.aop.Permission;
import open.tech.dawn.web.core.controller.BaseController;
import open.tech.dawn.web.core.support.Result;
import open.tech.dawn.web.util.ToolUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 角色表 前端控制器
 * </p>
 *
 * @author joss
 * @since 2019-11-07
 */
@RestController
@RequestMapping("/system/sysRole")
@Menu(name = "角色管理", icon = "fa fa-vcard", code = "role", url = "/views/system/role/role", order = 2, page = true, parentCode = "system")
public class SysRoleController extends BaseController {

    @Autowired
    private ISysUserService userService;

    @Autowired
    private ISysRoleService roleService;


    /**
     * 角色信息
     */
    @Permission
    @RequestMapping(value = "/info", method = {RequestMethod.GET})
    @Menu(name = "角色信息", code = "role_info", parentCode = "role")
    public Object info(Integer roleId) {
        if (ObjectUtil.isEmpty(roleId)) {
            throw new ServiceException("请求为空");
        }
        SysRole role = this.roleService.getById(roleId);
        Map<String, Object> map = new HashMap<>();
        map.put("role", role);
        map.put("pName", roleService.getRoleNameById(role.getPid()));
        return Result.createSuccess(map);
    }

    /**
     * 查看角色列表
     */
    @Permission
    @RequestMapping(value = "/list")
    @Menu(name = "查看角色列表", code = "role_list", parentCode = "role")
    public Object list(@RequestBody RoleQuery query) {
        Page<SysRole> page = new PageFactory<SysRole>().create(query);
        return new PageInfo<>(this.roleService.selectRoles(page, query));
    }

    /**
     * 添加角色
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @Permission
    @Menu(name = "添加角色", code = "role_add", parentCode = "role")
    public Object add(@RequestBody SysRole role) {
        String code = role.getCode();
        SysRole sysRole = new SysRole();
        sysRole.setCode(code);
        if (roleService.count(new QueryWrapper<>(sysRole)) > 0) {
            throw new ServiceException("角色编码已存在!");
        }
        role.setId(null);
        this.roleService.save(role);
        return Result.createSuccess();
    }

    /**
     * 编辑角色
     */
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @Permission
    @Menu(name = "编辑角色", code = "role_edit", parentCode = "role")
    public Object edit(@RequestBody SysRole role) {
        role.setCode(null);
        this.roleService.updateById(role);
        return Result.createSuccess();
    }

    /**
     * 删除角色
     */
    @RequestMapping(value = "/remove", method = RequestMethod.GET)
    @Permission
    @Menu(name = "删除角色", code = "role_remove", parentCode = "role")
    public Object remove(Integer roleId) {
        if (ObjectUtil.isEmpty(roleId)) {
            throw new ServiceException("请求为空");
        }

        SysRole sysRole = roleService.getById(roleId);

        //不能删除超级管理员角色
        if (Const.ADMIN_ROLE_CODE.equals(sysRole.getCode())) {
            throw new ServiceException("无权操作");
        }
        this.roleService.delRoleById(roleId);
        return Result.createSuccess();
    }


    /**
     * 配置角色权限
     */
    @RequestMapping("/setAuthority")
    @Permission
    @Menu(name = "配置角色权限", code = "role_setAuthority", parentCode = "role")
    public Object setAuthority(@RequestBody RoleAuthority roleAuthority) {
        if (ObjectUtil.isEmpty(roleAuthority.getRoleId())) {
            throw new ServiceException("请求为空");
        }
        //不能修改超级管理员角色
        if (Const.ADMIN_ROLE_ID.equals(Integer.toString(roleAuthority.getRoleId()))) {
            throw new ServiceException("无权操作");
        }
        this.roleService.setAuthority(roleAuthority.getRoleId(), roleAuthority.getIds());
        return Result.createSuccess();
    }

    /**
     * 获取树形图展示的角色列表
     */
    @RequestMapping(value = "/roleTreeList", method = RequestMethod.GET)
    @Permission
    @Menu(name = "获取树形图展示的角色列表", code = "role_roleTreeList", parentCode = "role")
    public Object roleTreeList() {
        List<ZTreeNode> roleTreeList = this.roleService.roleTreeList();
        roleTreeList.add(ZTreeNode.createParent());
        return Result.createSuccess(roleTreeList);
    }

    /**
     * 获取树形图展示的用户角色列表
     */
    @RequestMapping(value = "/roleTreeListByUserId", method = RequestMethod.GET)
    @Permission
    @Menu(name = "获取树形图展示的用户角色列表", code = "role_roleTreeListByUserId", parentCode = "role")
    public Object roleTreeListByUserId(String userId) {
        SysUser theUser = this.userService.getById(userId);
        String roleid = theUser.getRoleid();
        if (ToolUtil.isEmpty(roleid)) {
            return Result.createSuccess(this.roleService.roleTreeList());
        } else {
            String[] strArray = roleid.split(",");
            return Result.createSuccess(this.roleService.roleTreeListByRoleId(strArray));
        }
    }

}
